//
//  Slider.m
//  播放器模拟(列表)
//
//  Created by lanou3g on 15/11/3.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "Slider.h"
#import "UIView+Extension.h"


@interface Slider ()

@property (nonatomic, strong) UIView *totalProgressView;

@end

@implementation Slider

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        [self addGestureRecognizer];
        [self insertSubview:_totalProgressView belowSubview:_bufferView];
        self.totalProgressView.frame = CGRectMake(10, 10, kSliderWidth, 2);
        self.totalProgressView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.2];
    }
    return self;
}

- (void)addGestureRecognizer
{
    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(thumbSwiped:)];
    
    [self.thumbView addGestureRecognizer:panGR];
    NSLog(@"%@", _thumbView);
    _thumbView.userInteractionEnabled = YES;
}

- (void)thumbSwiped:(UIPanGestureRecognizer *)recognizer
{
    if ([(UIPanGestureRecognizer *)recognizer state] == UIGestureRecognizerStateBegan)
    {
        if ([_delegate respondsToSelector:@selector(progressSliderDidStart:value:)]) {
            [_delegate progressSliderDidStart:self value:_valueProgress];
        }
    }
    if ([(UIPanGestureRecognizer *)recognizer state] == UIGestureRecognizerStateChanged)
    {
        CGPoint curPoint = [recognizer locationInView:self];
        if (curPoint.x <= kThumbWidth / 2 || curPoint.x >= kSliderWidth + kThumbWidth / 2)
        {
            return;
        }
        
        _thumbView.center = CGPointMake(curPoint.x, _thumbView.center.y);
        _valueView.frame = CGRectMake(_valueView.frame.origin.x, _valueView.frame.origin.y, _thumbView.center.x , _valueView.frame.size.height);
        _valueProgress = [self transformXToValue:_thumbView.center.x];
    }
    
    if ([(UIPanGestureRecognizer *)recognizer state] == UIGestureRecognizerStateEnded)
    {
        if (_thumbView.center.x <= kThumbWidth / 2)
        {
            _valueProgress = 0.0f;
            _thumbView.center = CGPointMake(kThumbWidth / 2, _thumbView.center.y);
        }
        else if (_thumbView.center.x >= kSliderWidth + kThumbWidth / 2)
        {
            _valueProgress = 1.0f;
            _thumbView.center = CGPointMake(kSliderWidth + kThumbWidth / 2, _thumbView.center.y);
        }
        
        if ([_delegate respondsToSelector:@selector(progressSliderDidMoveEnd:value:)])
        {
            [_delegate progressSliderDidMoveEnd:self value:_valueProgress];
        }
    }

    
}

- (float)transformXToValue:(CGFloat)x
{
    return (x - kThumbWidth / 2) / kSliderWidth;
}

- (void)setBufferProgress:(CGFloat)bufferProgress
{
    _bufferProgress = bufferProgress;
    
    
    self.bufferView.frame = CGRectMake(10, 10, kSliderWidth * bufferProgress, 2);
    self.bufferView.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.2];
}

- (void)setValueProgress:(CGFloat)valueProgress
{
    _valueProgress = valueProgress;
    
    self.valueView.frame = CGRectMake(10, 10, kSliderWidth * valueProgress, 2);
    self.valueView.backgroundColor = [UIColor whiteColor];
    [self insertSubview:_valueView aboveSubview:_bufferView];
    
    self.thumbView.frame = CGRectMake(kSliderWidth * valueProgress, 0, 20, 20);
    [self insertSubview:_thumbView aboveSubview:_valueView];
}


#pragma mark - 懒加载
- (UIView *)bufferView
{
    if (_bufferView == nil) {
        _bufferView = [[UIView alloc] init];
        [self addSubview:_bufferView];
    }
    return _bufferView;
}

- (UIView *)valueView
{
    if (_valueView == nil) {
        _valueView = [[UIView alloc] init];
        [self addSubview:_valueView];
    }
    return _valueView;
}

- (UIView *)thumbView
{
    if (_thumbView == nil) {
        _thumbView = [[UIView alloc] init];
        [self addSubview:_thumbView];
    }
    return _thumbView;
}

- (UIView *)totalProgressView
{
    if (_totalProgressView == nil) {
        _totalProgressView = [[UIView alloc] init];
        [self addSubview:_totalProgressView];
    }
    return _totalProgressView;
}

@end
